from .models import deployInfo
from .models import deploy_history
from .models import work_order
from .models import deploy_sysconf_envs
from .models import deploy_sysconf_type
from rest_framework import serializers
from rest_framework.validators import UniqueTogetherValidator


class DeployDetailSerializer(serializers.ModelSerializer):
    '''
    获取应用列表
    '''

    create_time = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S", required=False, read_only=True)
    update_time = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S", required=False, read_only=True)

    class Meta:
        model = deployInfo
        fields = "__all__"


class DeployCreateSerializer(serializers.HyperlinkedModelSerializer):
    '''
    新增发布项目
    '''

    class Meta:
        model = deployInfo
        fields = ("name", "env", "application_type", "port", "git_repo", "branch", "domain", "params")
        '''
        验证数据是否已经存在
        '''
        validators = [
            UniqueTogetherValidator(
                queryset=deployInfo.objects.all(),
                fields=('name', "env", "application_type", "port", 'git_repo'),
                message="项目已存在"
            )
        ]


class ProjectNameListSerializer(serializers.ModelSerializer):
    '''项目名称和环境类型以及项目类型'''

    class Meta:
        model = deployInfo
        fields = ("name",)


class DeployHistorySerializer(serializers.ModelSerializer):
    '''
    历史构建
    '''

    create_time = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S")

    class Meta:
        model = deploy_history
        fields = "__all__"


class DeployWorkOrderDetailSerializer(serializers.ModelSerializer):
    '''获取工单'''

    plan_time = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S", label="预定发布时间", help_text="预定发布时间")
    create_time = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S", label="工单生成时间", help_text="工单生成时间")
    audit_time = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S", label="工单审核时间", help_text="工单审核时间")

    class Meta:
        model = work_order
        fields = "__all__"


class DeployWorkOrderCreateSerializer(serializers.ModelSerializer):
    '''创建工单'''

    plan_time = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S", label="预定发布时间", help_text="预定发布时间")

    class Meta:
        model = work_order
        fields = ("order_name", "order_type", "env", "submit_user", "status", "audit_user", "plan_time", "order_desc")


class DeployTypeConfDetailSerializer(serializers.ModelSerializer):
    # 项目类型
    create_time = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S", label="创建日期", help_text="创建日期")
    update_time = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S", label="更新日期", help_text="更新日期")

    class Meta:
        model = deploy_sysconf_type
        fields = ("__all__")


class DeployTypeConfCreateSerializer(serializers.ModelSerializer):
    # 创建类型

    class Meta:
        model = deploy_sysconf_type
        fields = ("name", "script_path", "desc")
        validators = [
            UniqueTogetherValidator(
                queryset=deploy_sysconf_type.objects.all(),
                fields=('name',),
                message="该类型已存在"
            )
        ]

    # 验证name名称为英文小写
    def validate_name(self, value):
        import re
        if not re.match('^[a-z]+$', value):
            raise serializers.ValidationError("该字段需要英文小写")
        return value


class DeployEnvsConfDetailSerializer(serializers.ModelSerializer):
    # 所属环境管理
    create_time = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S", label="创建日期", help_text="创建日期")
    update_time = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S", label="更新日期", help_text="更新日期")

    class Meta:
        model = deploy_sysconf_envs
        fields = ("__all__")


class DeployEvnsConfCreateSerializer(serializers.ModelSerializer):
    # 创建类型
    class Meta:
        model = deploy_sysconf_envs
        fields = ("name", "label")

        validators = [
            UniqueTogetherValidator(
                queryset=deploy_sysconf_envs.objects.all(),
                fields=('name',),
                message="该环境已存在"
            )
        ]

        # 验证name名称为英文小写
        def validate_name(self, value):
            import re
            if not re.match('^[a-z]+$', value):
                raise serializers.ValidationError("该字段需要英文小写")
            return value
